import  requests
import json
import time
import pymysql


url="https://api.json.wandou9.xyz:8036/movie/gethotmovie/page/1/rows/32.json"

#设计
urls = []
for i in range(1, 8):
    link = f"https://api.json.wandou9.xyz:8036/movie/gethotmovie/page/{i}/rows/32.json"
    urls.append(link)

print(urls)

#建立数据库连接
conn = pymysql.connect(host='192.168.18.13', port=33036, user='root', password='123456', db='chencanxian', charset='utf8')

cursor = conn.cursor()

for url in urls:
    #休息一秒
    time.sleep(1)
    #get
    resp = requests.get(url)
    # 检查S
    if resp.status_code == 200:
        data = json.loads(resp.text)
        movies = data['data']
        sql = "INSERT INTO movies (name, pic, actor, score, detail_link, movie_id) VALUES (%s, %s, %s, %s, %s, %s)"
        #
        for movie in movies:
            name = movie['hot_name']
            pic = movie['hot_img_url']
            actor = movie['movie_director']
            score = movie['hot_rate']
            detail_link = "https://wandou.la/" + movie['hot_url']
            movie_id = movie['hot_movie_id']
            val = (name, pic, actor, score, detail_link, movie_id)
            cursor.execute(sql, val)

    else:
        print("请求失败，状态码: ", resp.status_code)


conn.commit()
cursor.close()
conn.close()